package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Signup;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.TrainModel;

/**
 * Created by Lix on 2018/10/10.
 */

public class TrainSignUpActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tr_name, tr_differ, realname, phone, tr_unitprice, tr_price, people_num;
    private Button signup;
    private ImageView back, jia, jian;
    private SharedPreferences sp;
    private Context mContext;
    private int unitprice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_signup);
        initViews();
        initEvent();
        sp = getSharedPreferences("userinfo", MODE_PRIVATE);
        int tid = getIntent().getIntExtra("trainid", 1);
        int uid = sp.getInt("uid", 4);
        TrainModel trainModel = new TrainModel();
        trainModel.signup(getIntent().getIntExtra("trainid", 1), sp.getInt("uid", 4), listener);
        Toast.makeText(TrainSignUpActivity.this, "TID" + tid + "UID" + uid, Toast.LENGTH_SHORT).show();

    }

    TListener<Signup> listener = new TListener<Signup>() {
        @Override
        public void onResponse(Signup signup) {
            tr_name.setText(signup.getTrain_name());
            tr_differ.setText(leveltoString(signup.getDiffer()));
            tr_unitprice.setText(Integer.toString(signup.getUnit_price()));
            realname.setText(signup.getReal_name());
            phone.setText(Integer.toString(signup.getPhone()));
            tr_price.setText(Integer.toString(signup.getUnit_price()));
            people_num.setText("1");
            unitprice = signup.getUnit_price();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Success> signlistener = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            if (success.getError().equals("0")) {
                Toast.makeText(TrainSignUpActivity.this, "结算成功", Toast.LENGTH_SHORT).show();
                finish();
            } else
                Toast.makeText(TrainSignUpActivity.this, "余额不足", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };


    public String toAllPrice(int num) {

        int price = unitprice * num;
        String Price = Integer.toString(price);
        return Price;
    }


    private void initViews() {

        tr_name = (TextView) findViewById(R.id.ts_name);
        tr_differ = (TextView) findViewById(R.id.ts_differ);
        realname = (TextView) findViewById(R.id.ts_realname);
        tr_price = (TextView) findViewById(R.id.ts_price);
        phone = (TextView) findViewById(R.id.ts_phone);
        tr_unitprice = (TextView) findViewById(R.id.ts_unitprice);
        people_num = (TextView) findViewById(R.id.peonum);
        signup = (Button) findViewById(R.id.ts_signup);
        back = (ImageView) findViewById(R.id.ts_back);
        jia = (ImageView) findViewById(R.id.jia);
        jian = (ImageView) findViewById(R.id.jian);

    }

    private void initEvent() {
        signup.setOnClickListener(this);
        back.setOnClickListener(this);
        jia.setOnClickListener(this);
        jian.setOnClickListener(this);

    }

    public String leveltoString(int i) {
        String item = null;
        switch (i) {
            case 1:
                item = "入门级别";
                break;
            case 2:
                item = "菜鸟级别";
                break;
            case 3:
                item = "进阶级别";
                break;
            default:
                item = "菜鸟级别";
                break;
        }
        return item;
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ts_back:
                finish();
                break;
            case R.id.jia:
                if (Integer.parseInt(people_num.getText().toString()) == 5) {
                    Toast.makeText(TrainSignUpActivity.this, "每单仅限五人", Toast.LENGTH_SHORT).show();
                } else {

                    people_num.setText(Integer.toString(Integer.parseInt(people_num.getText().toString()) + 1));
                    tr_price.setText(toAllPrice(Integer.parseInt(people_num.getText().toString())));
                }
                break;
            case R.id.jian:
                if (Integer.parseInt(people_num.getText().toString()) == 1) {
                    Toast.makeText(TrainSignUpActivity.this, "最低选择一人", Toast.LENGTH_SHORT).show();
                } else {
                    people_num.setText(Integer.toString(Integer.parseInt(people_num.getText().toString()) - 1));
                    tr_price.setText(toAllPrice(Integer.parseInt(people_num.getText().toString())));
                }
                break;
            case R.id.ts_signup:
                TrainModel trainModel = new TrainModel();
                trainModel.jiesuan(getIntent().getIntExtra("trainid", 1), sp.getInt("uid", 4), Integer.parseInt(people_num.getText().toString()), signlistener);
                break;
        }

    }
}
